package com.zm.se.controller;

import com.zm.se.entity.Purchase;
import com.zm.se.entity.Scrap;
import com.zm.se.mapper.EquipmentMapper.EquMapper;
import com.zm.se.mapper.managerMapper.ManagerMapper;
import com.zm.se.service.EquService.impl.EquServiceImpl;
import com.zm.se.service.managerService.ex.UpdateException;
import com.zm.se.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("scrap")
public class ScrapController extends BaseController {
    @Autowired
    EquMapper equMapper;
    @Autowired
    ManagerMapper managerMapper;
    @Autowired
    EquServiceImpl service;
    @RequestMapping("addScrap")     //  器材名            器材数          器材品牌（单价根据品牌和器材名从eprice表i中查出）session查管理员
    public JsonResult<Scrap> addScrap(String ename, Integer enumber, String sup, HttpSession session){
        Integer price=equMapper.selectPriceByEnameSup(ename,sup);
        if (null==price){
            return new JsonResult<>("您填写的信息有误，请重新核对");
        }
        System.err.println(ename+"损坏"+enumber+"个,品牌为："+sup+"单价为："+price);

        String manager=managerMapper.findByNum(getNumFromSession(session)).getUsername();
        //调用service方法向scrap表和scrapnote表中插入信息
        service.addScrap(ename,enumber,price,sup,manager);
        return new JsonResult<>(OK);
    }

    @RequestMapping("outputScrapExcel")
    public JsonResult<String> outputScrapExcel(){
        service.outputScrapExcel();
        return new JsonResult<>(OK,"导出完毕，请返回桌面查看");
    }
    @RequestMapping("scraphistory")
    public JsonResult<List<Scrap>> ShowScrapHistory(Integer page){
        System.err.println(page);
        return new JsonResult<>(OK,equMapper.showScrap(page));
    }

    @RequestMapping("updatescrap")
    public JsonResult<Void> updateScrap(Integer id,String sup,Integer enumber,String ename,HttpSession session){
        System.err.println("updatescrapcontroller");
        System.err.println("id: "+id+" sup: "+sup+"  name:  "+ename+" number: "+enumber);
        String manager=managerMapper.findByNum(getNumFromSession(session)).getUsername();
        System.out.println(manager);

        System.err.println(service.scrapupdate(id,ename,enumber,sup,"zm"));
        return new JsonResult<>(OK);
    }
    @RequestMapping("addpurchase")
    public JsonResult<Void> addPurchase(Purchase purchase,HttpSession session){
        String manager=managerMapper.findByNum(getNumFromSession(session)).getUsername();

        System.out.println(purchase.getEname());
        System.out.println(purchase.getEnumber());
        System.out.println(purchase.getUnit());
        if (service.addPurchase(purchase,manager)==0){
            throw new UpdateException("插入失败，采购表中有这条数据，您可以直接修改");
        }
        return new  JsonResult<>(OK);
    }

    @RequestMapping("getscrapnotes")
    public JsonResult<Scrap> change(Integer id){
        System.err.println(id);
        Scrap scrap=equMapper.findScrapById(id);
        System.err.println(scrap);
        return new JsonResult<>(OK,scrap);
    }

    //////////////////////////////////////////////
    @RequestMapping("createpurchase")
    public JsonResult<Void> createPurchase(){
        return new JsonResult<>(OK);
    }
    @RequestMapping("purchase")
    public JsonResult<List<Purchase>> ShowPurchase(Integer page){
        System.err.println(page);
        return new JsonResult<>(OK,equMapper.showPurchase(page));
    }

    @RequestMapping("updatepurchase")
    public JsonResult<Void> updatePurchase(Integer id,String unit,Integer enumber,String ename,HttpSession session){
        System.err.println("updatePurchasecontroller");
        String manager=managerMapper.findByNum(getNumFromSession(session)).getUsername();
        System.err.println("id: "+id+" unit: "+unit+"  name:  "+ename+" number: "+enumber+"manager:"+manager);
        System.err.println(service.purchasepdate(id,ename,enumber,unit,manager));
        return new JsonResult<>(OK);
    }

    @RequestMapping("getpurchasenotes")
    public JsonResult<Purchase> changePurchase(Integer id){
        System.err.println(id);
       Purchase purchase=equMapper.findPurchaseById(id);
        System.err.println(purchase);
        return new JsonResult<>(OK,purchase);
    }

}
